﻿using ArduinoControllApp;
using ArduinoSharp.Lib;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Threading;

namespace ArduinoControllApp_WF
{
    public partial class Form1 : Form
    {
        static int channelCount = 8;

        private KeyHandler keyHandler = new KeyHandler();
        System.Media.SoundPlayer geigerCounterTickSound = new System.Media.SoundPlayer(@"C:\Users\Nic30\Documents\Visual Studio 2012\Projects\ArduinoControllApp\ArduinoControllApp_WF\Resources/art_beep1.wav");

        private int[] data = new int[channelCount];
        Timer tim = new Timer { Interval = 50 };
        Timer geigerCounterTickTimer = new Timer { Interval = 250 };

        public Form1()
        {
            InitializeComponent();
            this.chart1.ChartAreas[0].AxisY.Minimum = 0;
            this.chart1.ChartAreas[0].AxisY.Maximum = 1024;
            this.chart1.ChartAreas[0].AxisY.Interval = 128;

            this.serialPortUC1.onNewDataArived += new SerialNewDataHandler(actualize_data);
            tim.Tick += tim_Tick;
            tim.Start();
           
            geigerCounterTickTimer.Tick += geigerCounterTickTimer_tick;
            geigerCounterTickTimer.Start();

        }

        void geigerCounterTickTimer_tick(object sender, EventArgs e) 
        {
            geigerCounterTickSound.Play();
        }
        void tim_Tick(object sender, EventArgs e)
        {
            
            if (serialPortUC1.isOpened)
            {
                serialPortUC1.sendData(ArduinoCommand.CommandsPrefixes.SendAdc + ArduinoCommand.CommandDelimiter.ToString()+ "\n");
                serialPortUC1.sendData(keyHandler.Process());
                var maxWidth = this.chart1.Width;
                for (int i = 0; i < channelCount; i++)
                {
                    var num = data[i];
                    var adcStr = "ADC" + i;
                    if (chart1.Series[adcStr].Points.Count > maxWidth)
                    {
                        var firs = chart1.Series[adcStr].Points.First();
                        chart1.Series[adcStr].Points.Remove(firs);
                    }
                    chart1.Series[adcStr].Points.AddY(num);

                }
            }

        }

        public void actualize_data(object sender, SerialEventArgs e)
        {
            if (e.aData.HasValue)
            {
                this.data = e.aData.Data;
            }
        }

        public void Detach()
        {
            tim.Stop();
            //// Detach the event and delete the list
        }

        private void trackBar1_ValueChanged(object sender, EventArgs e)
        {
            var tb = sender as TrackBar;
            aquaGauge1.Value = tb.Value;
            geigerCounterTickTimer.Stop();
            geigerCounterTickTimer.Interval = tb.Value;
            geigerCounterTickTimer.Start();

        }
      




    }
    
}
